package com.heima.wemedia.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.common.exception.CustomException;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.dtos.SensitivePageReqDto;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.common.pojos.wemedia.WmSensitive;
import com.heima.wemedia.mapper.SensitiveMapper;
import com.heima.wemedia.mapper.WmSensitiveMapper;
import com.heima.wemedia.service.SensitiveService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class SensitiveServiceImpl extends ServiceImpl<WmSensitiveMapper,WmSensitive>  implements SensitiveService{
    @Autowired
    private SensitiveMapper sensitiveMapper;

    @Override
    public ResponseResult pageQuery(SensitivePageReqDto dto) {
        //1.参数校验
        if (dto==null){
            throw new  CustomException(AppHttpCodeEnum.PARAM_INVALID);
        }
        dto.checkParam();
        //2.组装条件Wrapper
        LambdaQueryWrapper<WmSensitive> wrapper = Wrappers.lambdaQuery();
        //3.按照频道名称模糊查询
        if (dto.getName()!=null) {
            wrapper.like(WmSensitive::getSensitives, dto.getName());
        }
        //4.按照创建时间倒序查询
        wrapper.orderByDesc(WmSensitive::getCreatedTime);
        //5.构建分页查询
        Page<WmSensitive> page=new Page(dto.getPage(),dto.getSize());
        page(page,wrapper);

        //6.封装结果返回
        PageResponseResult result = new PageResponseResult((int)page.getPages(),(int)page.getSize(),(int)page.getTotal());
        result.setData(page.getRecords());
        return result;

    }

    /**
     * 新增敏感词
     * @param wmSensitive
     * @return
     */
    @Override
    public ResponseResult saveSensitive(WmSensitive wmSensitive) {
        //1.参数校验
        if (wmSensitive==null){
            throw new CustomException(AppHttpCodeEnum.PARAM_INVALID);
        }
        //2.判断敏感词是否存在
        WmSensitive sensitive = getOne(Wrappers.<WmSensitive>lambdaQuery().eq(WmSensitive::getSensitives, wmSensitive.getSensitives()));
        if (sensitive!=null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"已存在的敏感词则不能保存");
        }
        wmSensitive.setCreatedTime(new Date());
        //3.保存数据
        save(wmSensitive);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);

    }

    /**
     * 修改敏感词
     * @param wmSensitive
     * @return
     */
    @Override
    public ResponseResult updateSensitive(WmSensitive wmSensitive) {
        //1.参数校验
        if (wmSensitive==null){
            throw new CustomException(AppHttpCodeEnum.PARAM_INVALID);
        }
        //2.修改参数
        updateById(wmSensitive);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    /**
     * 敏感词删除
     * @param id
     * @return
     */
    @Override
    public ResponseResult deleteSensitive(Integer id) {
        //1.参数校验
        if (id==null){
            throw new CustomException(AppHttpCodeEnum.PARAM_INVALID);
        }
        //2.检查数据是否存在
        WmSensitive wmSensitive = getById(id);
        if (wmSensitive==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        //3.删除数据
        removeById(id);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
}
